    //open file of SQLite
    rv = sqlite3_open(sqlite_filename, &db);
    if(rv != SQLITE_OK)
    {
        log_error("cannot open %s:%s\n", sqlite_filename, sqlite3_errmsg(db));
        sqlite3_close(db);
        exit(-1);
    }
    log_debug("open %s successfully!\n", sqlite_filename);

    //create table
    memset(sql, 0, sizeof(sql));
    snprintf(sql, sizeof(sql), "create table %s (time text, device text, temperature text);", table_name);
    rv = sqlite3_exec(db, sql, callback, NULL, &err_msg);
    if(rv != SQLITE_OK)
    {
        log_error("sql error:%s\n", err_msg);
        sqlite3_free(err_msg);
    }
    else
    {
        log_debug("create table %s successfully\n", table_name);
    }


//socket init
    if( (connect_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    {
        log_error("Use socket() to create a TCP socket failure: %s\n", strerror(errno));
        exit(-1);
    }
    log_debug("create socket successfully\n");

    memset(&servaddr, 0, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_port = htons(server_port);
    inet_aton(server_ip, &servaddr.sin_addr);

    connect_ok = connect(connect_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
    if(connect_ok < 0)
    {
        log_error("connect server failure:%s\n", strerror(errno));
    }
    else
    {
        log_debug("socket client init over:[connect fd:%d]\n", connect_fd);
    }







if(connect_ok < 0)//The network connection failed. Try reconnecting.
        {
            log_warn("connect [%d] falure:%s,Now try reconnecting!\n", connect_fd, strerror(errno));

            close(connect_fd);
            connect_fd = socket(AF_INET, SOCK_STREAM, 0);

            flags = fcntl(connect_fd, F_GETFL, 0);
            fcntl(connect_fd, F_SETFL, flags | O_NONBLOCK);//Connection timeout caused the alarm clock not to   be reset, thus affecting sampling.

            connect_ok = connect(connect_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
            sleep(1);//reconnect time
        }

        if(connect_ok < 0)//reconnect failure,insert data into SQLite
        {
            log_warn("reconnect failure\n");
            
            if(sample_flag)
            {
                log_debug("insert data into SQLite\n");

                memset(sql, 0, sizeof(sql));
                snprintf(sql, sizeof(sql), "insert into %s values('%s', '%s', '%s');",
                        table_name, data.sampling_time, data.sampling_device, data.temperature);

                rv = sqlite3_exec(db, sql, NULL, NULL, &err_msg);
                if(rv != SQLITE_OK)
                {
                    log_error("sql error:%s\n", err_msg);
                    sqlite3_free(err_msg);
                    continue;
                }
                check_table += 1;

                sample_flag = 0;

                continue;
            }
        }

if(sample_flag)
{
snprintf(upload_data, sizeof(upload_data), "%s/%s/%s/",
		data.sampling_time, data.sampling_device, data.temperature);
rv = write(connect_fd, upload_data, strlen(upload_data));
if(rv < 0)
{
	log_error("write sampled data failure:'%s',ignore SIGPIPE,Now save the sampled data to SQLite!  \n", strerror(errno));

	//ignore SIGPIPE, insert data into SQLite
	memset(sql, 0, sizeof(sql));
	snprintf(sql, sizeof(sql), "insert into %s values('%s', '%s', '%s');",
			table_name, data.sampling_time, data.sampling_device, data.temperature);

	rv = sqlite3_exec(db, sql, NULL, NULL, &err_msg);
	if(rv != SQLITE_OK)
	{
		log_error("sql error:%s\n", err_msg);
		sqlite3_free(err_msg);
		continue;
	}

	sample_flag = 0;
	continue;
}
log_debug("write %d sampled data:'%s' successfully\n", rv, upload_data);

sample_flag = 0;
}

if((connect_ok > -1) && (check_table > 0))
        {
            memset(&callback_arg, 0, sizeof(callback_arg));

            memset(sql, 0, sizeof(sql));
            snprintf(sql, sizeof(sql), "select * from %s limit 1;", table_name);

            //check table in SQLite
            rv = sqlite3_exec(db, sql, callback, (void *)&callback_arg, &err_msg);
            if(rv != SQLITE_OK)
            {
                log_error("sql error:%s\n", err_msg);
                sqlite3_free(err_msg);
                continue;
            }

            if(callback_arg.sqlite_have_data)//there is data in SQLite
            {
                callback_arg.delete_sqlite_data = 1;//delete SQLite data
                //不应该放到该结构体里

                check_table += 1;

                rv = write(connect_fd, callback_arg.upload_sqlite_data,
                        strlen(callback_arg.upload_sqlite_data));
                if(rv < 0)//ignore SIGPIPE, save data in the SQLite
                {
                    log_error("write data in SQLite failure:'%s', ignore SIGPIPE, don't delete!\n",             strerror(errno));
                    callback_arg.delete_sqlite_data = 0;
                    continue;
                }
                log_debug("write data in SQLite:%s successfully\n", callback_arg.upload_sqlite_data);

                //(或许能在callback中实现删除)
                if(callback_arg.delete_sqlite_data)//write successfully, delete data in the SQLite
                {
                    log_debug("now delete data already upload in the SQLite\n");

                    memset(sql, 0, sizeof(sql));
                    snprintf(sql, sizeof(sql), "delete from %s where rowid in(select rowid from %s limit 1);",
                            table_name, table_name);
					rv = sqlite3_exec(db, sql, NULL, NULL, &err_msg);
                    if(rv != SQLITE_OK)
                    {
                        log_error("sql error:%s\n", err_msg);
                        sqlite3_free(err_msg);
                        continue;
                    }
                    callback_arg.sqlite_have_data = 0;
                }
            }
            else
            {
                check_table -= 1;
            }
		}
